package com.TTBook.impl.member.serviceImpl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.providers.dao.SaltSource;
import org.springframework.security.providers.encoding.PasswordEncoder;

import com.TTBook.core.member.dao.MemberInfoDAO;
import com.TTBook.core.member.model.MemberInfo;
import com.TTBook.core.member.service.MemberInfoService;
import com.TTBook.utils.BookBaseServiceException;
import com.TTBook.utils.LogConstants;
import com.TTBook.utils.SerialNoConstant;
import com.TTBook.utils.StatusConstants;
import com.TTBook.utils.keygen.SerialGeneratorMgr;
import com.TTBook.utils.keygen.SerialGeneratorMgrAware;

public class MemberInfoServiceImpl implements MemberInfoService,SerialGeneratorMgrAware {
	
	private SerialGeneratorMgr serialGeneratorMgr;
	private MemberInfoDAO memberInfoDAO;
	private SaltSource saltSource;
	private PasswordEncoder passwordEncoder;
	

	public void setSerialGeneratorMgr(SerialGeneratorMgr serialGeneratorMgr) {
		this.serialGeneratorMgr = serialGeneratorMgr;
	}
	public MemberInfoDAO getMemberInfoDAO() {
		return memberInfoDAO;
	}
	public void setMemberInfoDAO(MemberInfoDAO memberInfoDAO) {
		this.memberInfoDAO = memberInfoDAO;
	}
    
	public void setSaltSource(SaltSource saltSource) {
		this.saltSource = saltSource;
	}
	public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
		this.passwordEncoder = passwordEncoder;
	}
	@Override
	public void addMemberInfo(MemberInfo memberInfo) throws BookBaseServiceException
	{
		 if(memberInfo == null) throw new BookBaseServiceException(LogConstants.MemberLogCode.MEM_REG_0001+"--->"
				 +LogConstants.MemberLogDetail.MEM_REG_0001_DEL, "memberInfo对象不能为空!!!");
		 if(StringUtils.isBlank(memberInfo.getLoginName())) throw new 
		 BookBaseServiceException(LogConstants.MemberLogCode.MEM_REG_0001+"--->"
				 +LogConstants.MemberLogDetail.MEM_REG_0001_DEL, "用户名不能为空!!!");
		if(CheckLoginNameExist(memberInfo.getLoginName())) throw new 
		BookBaseServiceException(LogConstants.MemberLogCode.MEM_REG_0001+"--->"
				+LogConstants.MemberLogDetail.MEM_REG_0001_DEL, "登陆名已存在!!!");
		if(StringUtils.isBlank(memberInfo.getPassword()))throw new 
		BookBaseServiceException(LogConstants.MemberLogCode.MEM_REG_0001+"--->"
				 +LogConstants.MemberLogDetail.MEM_REG_0001_DEL, "密码不能为空!!!");
		String userId = serialGeneratorMgr.getSerialKey(SerialNoConstant.MEMBER_ID_SERIAL);
		memberInfo.setUserid(userId);
		memberInfo.setRegistTime(new Date());
		memberInfo.setPassword(encryptPassword(memberInfo.getPassword()));
		memberInfo.setStatus(StatusConstants.MemberStatus.MEMBER_STATUS_1);
		this.memberInfoDAO.insert(memberInfo);
	}
	/**
	 * 
	 * <b>Method Function Description:</b><br>
	 * @param loginName
	 * @return
	 * @throws BookBaseServiceException
	 * <br><b>Method Logic Description:</b>
	 * TODO 检查登录名是否存在
	 */
	public boolean CheckLoginNameExist(String loginName)throws BookBaseServiceException
	{
		boolean flag = false;
		if(loginName != null)
		{
			List<MemberInfo> member = this.memberInfoDAO.selectByLoginName(loginName);
			if(member.size() >0 ){
				flag = true;
			}
		}
		return flag;
	}
	
	public String encryptPassword(String pwd) throws BookBaseServiceException{
		if(passwordEncoder != null && saltSource != null && pwd != null){
			pwd = passwordEncoder.encodePassword(pwd, saltSource);
			if(pwd == null)
			{
				throw new BookBaseServiceException(LogConstants.SecurityLogCode.SECURITY_REG_ENCRYPT_0001
						+LogConstants.SercurityDetail.SECURITY_REG_ENCRYPT_0001_DEL, "用户密码无法加密");
			}
		}
		return pwd;
	}
}
